package com.burgeon.backend.EnterpriseWeChat.service.Impl;

import com.burgeon.backend.EnterpriseWeChat.mappers.EWQyCodeMapper;
import com.burgeon.backend.EnterpriseWeChat.rest.FAB.getProductByCode;
import com.burgeon.backend.EnterpriseWeChat.service.EWGoodsService;
import com.burgeon.backend.SHOP.service.SystemService;
import com.burgeon.backend.SHOPBACKEND.entity.Android.EmpBindingRecord;
import com.burgeon.backend.SHOPBACKEND.entity.*;
import com.burgeon.backend.SHOPBACKEND.entity.FAB.FABVo;
import com.burgeon.backend.SHOPBACKEND.mappers.AndroidMapper;
import com.burgeon.backend.SHOPBACKEND.mappers.FabMapper;
import com.burgeon.backend.SHOPBACKEND.mappers.MaterialMapper;
import com.burgeon.backend.SHOPBACKEND.rest.ModVipLogo;
import com.burgeon.backend.SHOPBACKEND.service.ex.*;
import com.burgeon.backend.Util.CodeImageUtil;
import com.burgeon.backend.WeChat.GetAccesToken;
import com.burgeon.backend.WeChat.Wxacode;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.entity.User;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.mappers.UserMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class EWGoodsServiceImpl implements EWGoodsService {
    private final static Log logger = LogFactory.getLog(EWGoodsService.class);
    @Autowired
    private AndroidMapper androidMapper;
    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private GetAccesToken getAccesToken;
    @Autowired
    private Wxacode wxacode;
    @Autowired
    private FabMapper fabMapper;
    @Autowired
    private SystemService systemService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private EWQyCodeMapper ewQyCodeMapper;

    public String getAccount(String url){
        System.err.println("IP穿"+url);
        String token = androidMapper.GetTokenByUrl(url);
        System.err.println(token);
        String account = androidMapper.GetTokenByToken(token);
        System.err.println(account);
        return account;

    }

    @Override
    public String createQRcode(String token, String id) throws ErrorDataException {
        try {
            String account = accountMapper.GetAccountForToken(token);
            String accessToken = systemService.GetAccesToken(account);
            // String filePath = "//root//code/img/" + "empRQcode/" + account +"/" + id + ".png";
            String linkImg = wxacode.createEmpRQcode(id,accessToken,account);
            EmpBindingRecord empBindingRecord = new EmpBindingRecord();
            empBindingRecord.setUrl(linkImg);
            empBindingRecord.setAccount(account);
            empBindingRecord.setEmpId(id);
            androidMapper.createErpBingingRecord(empBindingRecord);
            return linkImg;
        } catch (ErrorDataException e) {
            e.printStackTrace();
            throw new ErrorDataException(e.getMessage());
        }


    }

    @Override
    public HashMap judgeQRcode(String token, String id)  throws ErrorDataException, InexistenceException {
        String account = accountMapper.GetAccountForToken(token);
        HashMap hashMap = new HashMap();
        EmpBindingRecord erpBingingRecord = androidMapper.findErpBingingRecord(id, account);
        String card = ewQyCodeMapper.getQyCardByEmpNo(id,token);
        logger.info("绑定URL======"+erpBingingRecord);
        if (null != erpBingingRecord) {
            hashMap.put("bingRecord",erpBingingRecord.getUrl());
        }else {
            hashMap.put("bingRecord","");
        }
        if(card!=null){
            hashMap.put("qyCard",card);
        }else {
            hashMap.put("qyCard","");
        }

        return hashMap;
    }

    private Info GetInfo(String token) {
        return accountMapper.GetTokenData(token);
    }

    /**
     * 上传VIP头像到erp
     * @param file
     * @param token
     */
    @Override
    public String upVipImg(MultipartFile file, String token, String erpVid,String openId,String uCardNo) {
        if (null == file || file.isEmpty()) {
            System.err.println("文件为空");
            throw new UploadException("文件为空");
        }else if (null == token || token.isEmpty()){
            throw  new UploadException("token");
        }
        String account = accountMapper.GetAccountForToken(token);
        Info info = GetInfo(account);
        System.err.println(info);
        //文件名
        String fileName = file.getOriginalFilename();
        System.err.println(fileName);
        // 后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 上传后的路径
        String filePath = "//root//code//img//" + account + "-vip//" + uCardNo+ "//";
        // 新文件名
        fileName = uCardNo + suffixName;
        saveFile(file, fileName, filePath);
        //上传的头像存到erp
        String url ="http://lfwdcdn.cloudsmicro.com/img/"+ account + "-vip/"+ uCardNo+ "/"+fileName;

        ModVipLogo modVipLogo = new ModVipLogo(info.getUrl(),info.getAccount(),info.getPassword());
        //判断是否微信小程序会员
        logger.info("openId:"+openId);
        if(!"null".equals(openId)){
            logger.info("openId是空");
            User user = userMapper.FindUserByAppOpenId(openId,account);
            if(null != user){
                //将拍照的头像存到数据库
                //System.err.println("有user");
                logger.info("有user");
                androidMapper.insertAppHeadImg(user.getId(),url,account);
            }

            try {
                //将拍照的头像存到erpLOGO_URL
                //System.err.println("好吃的思考存到erp");
                modVipLogo.getData(erpVid,url);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }else {
            //System.err.println("jkllll");
            //只将拍照的头像存到erp，根据id修改erp,将头像加进去
            try {
                System.err.println("存到erp");
                logger.info("没有openId");
                modVipLogo.getData(erpVid,url);
            } catch (Exception e)  {
                e.printStackTrace();
            }
        }


            return url;

    }
    /***************************************商城品牌分组***************************************************************/
    /**
     * 获取所有商城显示的品牌分组
     * @param token
     * @return
     */
    @Override
    public ArrayList<BrandApp> getBrandByIp(String token) {
        if (null == token || token.isEmpty()){
            throw  new UploadException("没传token");
        }

        ArrayList<BrandApp> brandApps = androidMapper.getBrandByAccount(token);

        if(null == brandApps){
            throw new UserNotFoundException("没有分组");
        }
        return brandApps;
    }

    /**
     * 搜索
     * @param token
     * @param name
     * @return
     */
    @Override
    public ArrayList<BrandApp> findBrandByName(String token, String name) {
        if (null == token || token.isEmpty()){
            throw  new UploadException("没有穿ip");
        }

        ArrayList<BrandApp> brandApps = androidMapper.findBrandByName(token,name);
        return brandApps;
    }

    @Override
    public List<LinkGoodsVo> GetGoodsLinkByBrandId(String brandId, String token, Integer page, String daoId) {
        if (null == token || token.isEmpty()){
            throw  new UploadException("没有传token");
        }else  if (null == brandId || brandId.isEmpty()){
            throw  new UploadException("没有传cIId");
        }else if (null == page){
            throw  new UploadException("没有穿传start");
        }
        token = token.replace(" ","");

        Integer start = (page-1)*20;
        List<Goods> goods = androidMapper.getGoodsByBrandId(brandId,start,token);
        System.err.println("service"+goods);
        if(null == goods || goods.size() == 0){
            throw new UploadException("没有商品");
        }
        List<LinkGoodsVo> linkGoodsVos = new ArrayList<>();
        for (Goods g:goods) {
            String accessToken = systemService.GetAccesToken(token);
            //先从数据库查小程序码
            ArrayList<String> linkImg = androidMapper.GetGoodLink(g.getId(),daoId,token);
            System.err.println(linkImg);
            String img = "";
            LinkGoodsVo l = new LinkGoodsVo();
            if(linkImg.size()>0){
                l.setImg(linkImg.get(0));

            }else {
                //获取商品小程序码
                img = wxacode.getminiqrQr(g.getId(),accessToken,token,daoId);
                l.setImg(img);
            }


            //获取商品banner图
            List<String> banner =  androidMapper.getBanner(g.getId());
            List<String> banner2 = new ArrayList<>();
            for (String b : banner) {
                String a= b.replace("http://lfwdcdn","https://lfwd");
                banner2.add(a);
            }
//            LinkGoodsVo l = new LinkGoodsVo();
            l.setId(g.getId());
            l.setCode(g.getCode());
            l.setName(g.getName().replace("&",""));
            l.setBanner(banner2);
//            l.setImg(linkImg);
//            l.setPresentation(g.getPresentation());
            l.setPrice(g.getPrice());
            linkGoodsVos.add(l);
        }

        return linkGoodsVos;
    }

    /**********************************************商城分组*******************************************************/

    //商城分组
    @Override
    public List<ClassifyVo> getClassify(String token) {
        if (null == token || token.isEmpty()){
            throw  new UploadException("没有穿token");
        }

        List<ClassifyVo> classifies = androidMapper.getClassify(token);
        if(null == classifies){
            throw new UserNotFoundException("没有分组");
        }
//
//        for (Classify classify : classifies) {
//            //根据大分类的id查询小分类
//            List<ClassifyItem> classifyItems = androidMapper.getClaItemByCid(classify.getId());
//           // System.err.println(classifyItems);
//
//            classify.setClassifyItem(classifyItems);
//        }
        System.err.println("service"+classifies);
        return classifies;
    }

    @Override
    public List<ClassifyVo> findByName(String token, String name) {
        if (null == token || token.isEmpty()){
            throw  new UploadException("没有穿token");
        }
        List<ClassifyVo> classifyVos = androidMapper.findByName(token,name);
        return classifyVos;
    }



    /**
     * 获取商品根据小分类id
     */
    @Override
    public List<LinkGoodsVo> getGoodsLink(String cIId, String token, Integer start, String daoId) {
        if (null == token || token.isEmpty()){
            throw  new UploadException("没有穿ip");
        }else  if (null == cIId || cIId.isEmpty()){
            throw  new UploadException("没有穿cIId");
        }else if (null == start){
            throw  new UploadException("没有穿start");
        }
        token = token.replace(" ","");
        start = (start-1)*20;
        System.err.println("start=="+cIId);
        //获取商品
        List<Goods> goods = androidMapper.getGoodsAll(cIId,start,token);
//        System.err.println(account);
        System.err.println("service"+goods);
        if(null == goods || goods.size() == 0){
            throw new UploadException("没有商品");
        }
        List<LinkGoodsVo> linkGoodsVos = new ArrayList<>();
        for (Goods g:goods) {

            //System.err.println("获取"+token);
           // String accessToken = getAccesToken.GetAccesToken(token);
            String accessToken = systemService.GetAccesToken(token);
            //先从数据库查小程序码
            System.err.println(g.getId());
            ArrayList<String> linkImg = androidMapper.GetGoodLink(g.getId(),daoId,token);
            System.err.println(linkImg);
            String img = "";
            LinkGoodsVo l = new LinkGoodsVo();
            if(linkImg.size()>0){
                l.setImg(linkImg.get(0));

            }else {
                //获取商品小程序码
                img = wxacode.getminiqrQr(g.getId(),accessToken,token,daoId);
                l.setImg(img);
            }


            //获取商品banner图
            List<String> banner =  androidMapper.getBanner(g.getId());
            List<String> banner2 = new ArrayList<>();
            for (String b : banner) {
                String a= b.replace("http://lfwdcdn","https://lfwd");
                banner2.add(a);
            }

            l.setId(g.getId());
            l.setCode(g.getCode());
            l.setName(g.getName().replace("&",""));
            l.setBanner(banner2);
//            l.setImg(img);
//            l.setPresentation(g.getPresentation());
            l.setPrice(g.getPrice());
            linkGoodsVos.add(l);
        }

        return linkGoodsVos;
    }


  //合并二维码和图片

public void merg(String bigPath,String smallPath,String x,String y){
    try {
        CodeImageUtil.mergeImage(bigPath,smallPath,"20","20");
    } catch (IOException e) {
        e.printStackTrace();
    }
}




    //根据code搜索
    @Override
    public List<LinkGoodsVo> findByCode(String code, Integer start, String token, String daoId) {
        if (null == token || token.isEmpty()){
            throw  new UploadException("没有穿tokenip");
        }
        token = token.replace(" ","");
        start = (start-1)*20;
        List<Goods> goods = androidMapper.findByCode(code,start,token);
        if(null == goods || goods.size() == 0){
            throw new UploadException("没有商品");
        }
        List<LinkGoodsVo> linkGoodsVos = new ArrayList<>();
        for (Goods g:goods) {

            System.err.println("获取"+token);
            //String accessToken = getAccesToken.GetAccesToken(token);
            String accessToken = systemService.GetAccesToken(token);
            //先从数据库查小程序码
            ArrayList<String> linkImg = androidMapper.GetGoodLink(g.getId(),daoId,token);
            System.err.println(linkImg);
            String img = "";
            LinkGoodsVo l = new LinkGoodsVo();
            if(linkImg.size()>0){
                l.setImg(linkImg.get(0));

            }else {
                //获取商品小程序码
                img = wxacode.getminiqrQr(g.getId(),accessToken,token,daoId);
                l.setImg(img);
            }

            //获取商品banner图
            List<String> banner =  androidMapper.getBanner(g.getId());
            List<String> banner2 = new ArrayList<>();
            for (String b : banner) {
                String a= b.replace("http://lfwdcdn","https://lfwd");
                banner2.add(a);
            }

//            LinkGoodsVo l = new LinkGoodsVo();
            l.setId(g.getId());
            l.setCode(g.getCode());
            l.setName(g.getName().replace("&",""));
            l.setBanner(banner2);
//            l.setImg(linkImg);
//            l.setPresentation(g.getPresentation());
            l.setPrice(g.getPrice());
            System.err.println(g.getPrice());
            linkGoodsVos.add(l);
        }

        return linkGoodsVos;
    }


    /**+++++++++++++++++++++++++++++++++++商城分组++++++++++++++++++++++++++++++++++++++*/
    //新增素材分组
    @Override
    public Integer insertMaterialGroup(MultipartFile file,String groupName, String token) {
        String types = "group";
        if (null == file || file.isEmpty()) {
            System.err.println("文件为空");
            throw new UploadException("文件为空");
        }

        //文件名
        String fileName = file.getOriginalFilename();
        // 后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //http://lfwd.cloudsmicro.com:8080/img/mgg-shop/2.png

        //获取token
        //String token = accountMapper.GetTokenForAccount(account);
        try {
            MaterialGroup materialGroup = new MaterialGroup();
            materialGroup.setGroupName(groupName);
           // materialGroup.setGroupImg("http://lfwd.cloudsmicro.com:8080/img/" + account + "-material/" + groupName + "/" + groupName + suffixName);
            materialGroup.setAccount(token);
            //新增
            Integer row = materialMapper.insertMaterialGroup(materialGroup);
            materialGroup.setGroupImg("http://lfwd.cloudsmicro.com:8080/img/" + token + "-material/" + materialGroup.getId() + "/" + materialGroup.getId() + suffixName);
            //将图片修改
            materialMapper.upGroup(materialGroup.getId(),materialGroup.getGroupImg());
            // 上传后的路径
            String filePath = "//root//code//img//" + token + "-material//" + materialGroup.getId() + "//";
            // 新文件名
            fileName = materialGroup.getId() + suffixName;
            saveFile(file, fileName, filePath);
            return row;
        }catch (Exception e){
            e.printStackTrace();
            throw new InsertException("新增错误");
        }
       // return null;
    }

    //保存文件
    private void saveFile(MultipartFile multipartFile, String fileName, String filePath) {
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            multipartFile.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("保存文件出错"+e.getMessage());
            throw new UploadException("系统错误");
        } catch (Exception e) {
            logger.error("保存文件出错"+e.getMessage());
            System.err.println(e.getMessage());
            throw new UploadException("系统错误");
        }
    }

    //查看所有分组
    @Override
    public ArrayList<MaterialGroup> findGroupByAccount(String ip) {
        String account = getAccount(ip);
        if(null == account){
            throw new DataNotFoundException("没有"+ip);
        }
      //  int start = (page-1)*20;
        //根据account查看分组
        //ArrayList<MaterialGroup> materialGroups = materialMapper.findGroupByAccount(account,start);
        ArrayList<MaterialGroup> materialGroups = materialMapper.findAnGroupByAccount(account);
        if(null == materialGroups){
            throw new DataNotFoundException("没有品牌分组 ");
        }
        return materialGroups;
    }

    //删除分组
    @Override
    public Integer deGroupById(String id, String ip) {
        String account = getAccount(ip);
        if(null == account){
            throw new DataNotFoundException("没有"+ip);
        }
        Integer row = materialMapper.deGroupById(id,account);
        if(row == 0){

            logger.info("删除失败");
            throw new DeleteException("删除失败了");
        }
        return row;
    }

    //编辑分组
    @Override
    public Integer upGroupName(MultipartFile file,String id, String groupName,String ip) {
//        if (null == file || file.isEmpty()) {
//            System.err.println("文件为空");
//            throw new UploadException("文件为空");
//        }
        String account = getAccount(ip);
        if(null == account){
            throw new DataNotFoundException("没有"+ip);
        }
        //文件名
        String fileName = file.getOriginalFilename();
        // 后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //http://lfwd.cloudsmicro.com:8080/img/mgg-shop/2.png
        // 上传后的路径
        String filePath = "//root//code//img//" + account + "-material//" + id + "//";
        // 新文件名
        fileName = id + suffixName;
        saveFile(file, fileName, filePath);
        String groupImg = "http://lfwd.cloudsmicro.com:8080/img/" + account + "-material/" + id + "/" + id + suffixName;
        Integer row =  materialMapper.upGroupName(id,groupName,groupImg);
        logger.info(row);
        if(row == 0){

            throw new UpDataException("修改失败");
        }
        return row;
    }

    /**
     * 搜索分组
     * @param ip
     * @param groupName
     * @return
     */
    @Override
    public ArrayList<MaterialGroup> findGroupByName(String ip, String groupName) {
        String account = getAccount(ip);
        if(null == account){
            throw new DataNotFoundException("没有"+ip);
        }
       // int start = (page-1)*20;
        //根据account查看分组
        //ArrayList<MaterialGroup> groups = materialMapper.findGroupByName(account,groupName,start);
        ArrayList<MaterialGroup> groups = materialMapper.findAnGroupByName(account,groupName);
        return groups;
    }

    /**
     * 改变顺序
     * @param beforeId
     * @param afterId
     * @return
     */
    @Override
    public Integer upGroupId(String beforeId, String afterId) {
       int r =  androidMapper.upGroupId(beforeId,afterId);
       int a =  androidMapper.upGroupId(afterId,beforeId);
       int s = r+a;
        if (s != 2){
            throw new UpDataException("没有改变顺序");
        }
        return s;
    }

    /**+++++++++++++++++++++++++++++++++++++++++品牌素材++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
    @Override
    public Integer insertMaterial(MultipartFile[] files, String materialName, String groupId,
                                  String imgDescribe,  String ip,String price) {
        if (null == ip || ip.isEmpty()){
            throw  new UploadException("没有穿ip");
        }
        String account = getAccount(ip);
        System.err.println(account);
        if(null == account ){
            throw new DataNotFoundException("没有"+ip);
        }
        String types = "material";
        String fs = Arrays.toString(files);
        System.err.println(files.length);
        if (files.length == 0) {
            throw new UploadException("图片为空");
        }
        //根据groupId查询分组名字
        MaterialGroup materialGroup = materialMapper.fGroupNameByGpId(account,groupId);
        //素材
        Material m = new Material();
        //素材名称
        m.setMaterialName(materialName);
        //所属分组
        m.setGroupId(groupId);
        //图片描述
        m.setImgDescribe(imgDescribe);
        //商品id
        //m.setGoodsId(goodsId);
        //新增素材
        Integer row = materialMapper.insertMaterial(m);
        //新增素材图片
        int s = addMaImgs(files,materialName,materialGroup,m.getId(),account,m.getGroupId());
        System.err.println(s);
        return row;
    }


    @Override
    public Integer upMaterial(Material material) {
        return null;
    }



    @Override
    public MaterialVo findMaterialById(String id) {
        return null;
    }

    //查看分组下的素材
    @Override
    public ArrayList<MaterialEVo> getMaterialByGroupId(String ip, String groupId, String daoId) {
        if (null == ip || ip.isEmpty()){
            throw  new UploadException("没有穿ip");
        }
        String account = getAccount(ip);
        System.err.println(account);
        if(null == account ){
            throw new DataNotFoundException("没有"+ip);
        }
        ArrayList<MaterialEVo> materialEVoList = new ArrayList<>();

        //查看所有素材
        ArrayList<Material> materials = materialMapper.findMaterialBiGid(account,groupId);
        //System.err.println("分组素材"+materials);
        for (Material material:materials) {
            String id = material.getId();
            String link = "http://lfwdcdn.cloudsmicro.com/H5/Fab.html?id="+id+"&daoId="+daoId;
            material.setLink(link);
        }

        return getMaterialEVos(materialEVoList, materials,account);
    }

    /**
     * 查看所有素材
     * @param ip
     * @return
     */
    @Override
    public ArrayList<MaterialEVo> getMaterialAll(String ip, String daoId) {
        if (null == ip || ip.isEmpty()){
            throw  new UploadException("没有穿ip");
        }
        String account = getAccount(ip);
        System.err.println("所有素材"+account);
        if(null == account ){
            throw new DataNotFoundException("没有"+ip);
        }

        ArrayList<MaterialEVo> materialEVoList = new ArrayList<>();
        //int start = (page-1)*20;
        //查看所有素材
        //ArrayList<Material> material = materialMapper.findMaterialAll(account,start);
        ArrayList<Material> materials = materialMapper.findAnMaterialAll(account);
        for (Material material:materials) {
            String id = material.getId();
            String link = "http://lfwdcdn.cloudsmicro.com/H5/Fab.html?id="+id+"&daoId="+daoId;
            material.setLink(link);
        }

        return getMaterialEVos(materialEVoList, materials,account);
    }

    /**
     * 搜索素材
     * @param ip
     * @param materialName
     * @return
     */
    @Override
    public ArrayList<MaterialEVo> findMaterialByName(String ip, String materialName, String daoId) {
        if (null == ip || ip.isEmpty()){
            throw  new UploadException("没有穿ip");
        }
        String account = getAccount(ip);
        System.err.println(account);
        if(null == account ){
            throw new DataNotFoundException("没有"+ip);
        }

        ArrayList<MaterialEVo> materialEVoList = new ArrayList<>();
        //搜索
        ArrayList<Material> materials = materialMapper.findMaterialByName(account,materialName);
        for (Material material:materials) {
            String id = material.getId();
            String link = "http://lfwdcdn.cloudsmicro.com/H5/Fab.html?id="+id+"&daoId="+daoId;

            material.setLink(link);
        }

        return getMaterialEVos(materialEVoList, materials,account);
    }


    //将素材和素材图片整合到一起的方法
    private ArrayList<MaterialEVo> getMaterialEVos(ArrayList<MaterialEVo> materialEVoList, ArrayList<Material> materials, String account) {

        for (Material m:materials) {

            MaterialEVo materialEVos = new MaterialEVo();
            //获取微信小程序的logo
            String logo = androidMapper.getWeiLogo(account);
            materialEVos.setLogo(logo);

            //根据素材id查看素材图片
            ArrayList<MaterialImgs> materialImgs = materialMapper.findImgsByMId(m.getId());

            if(null == materialImgs || materialImgs.size()==0){
                logger.info("没有图片");
                materialEVoList.add(materialEVos);
            }
            //里层图片
            List imgList = new ArrayList();
            for (MaterialImgs img: materialImgs) {
                imgList.add(img.getImgs());

            }
            logger.info(imgList);
            m.setImgList(imgList);
            logger.info(m);
            materialEVos.setMaterial(m);
            if(materialImgs.size() != 0){
                materialEVos.setImg(materialImgs.get(0).getImgs());
                materialEVoList.add(materialEVos);
            }else {
                materialEVoList.add(materialEVos);
            }

        }
        return materialEVoList;
    }

    //新增素材图片
    private int addMaImgs(MultipartFile[] files, String materialName, MaterialGroup materialGroup, String mid, String account, String groupId){
        int c = 0;
        int s = 0;
        //遍历图片
        for (MultipartFile multipartFile : files) {
            // 文件名
            String fileName = multipartFile.getOriginalFilename();
            // 后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            // 上传后的路径
            String filePath = "//root//code//img//" + account + "-material//"+ groupId + "//"+mid+"//";
            // 新文件名
            fileName = mid + "-" + ++c + suffixName;
            //保存
            saveFile(multipartFile,fileName,filePath);
            //新增到素材图片
            String img = "http://lfwd.cloudsmicro.com:8080/img/" + account + "-material/" + groupId +"/"+mid+"/"+mid + "-" + c + suffixName;
            int row = materialMapper.insertMaterialImgs(mid,img);
            s=s+row;
        }
        return s;
    }

    /**++++++++++++++++++++++++++++++++++++买点培训+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++**/
    //根据款号查询卖点培训
    @Override
    public FABVo findDataByCode(String code, String token) {
        if (null == token || token.isEmpty()){
            throw  new UploadException("没有穿ip");
        }
        String account = accountMapper.GetAccountForToken(token);
        //根据款号查询卖点培训
        FABVo data = androidMapper.findDataByCode(account,code);
        System.err.println(data);
        if (null != data){
            data.setBanner(androidMapper.getImgFAB(data.getId()));
        }

        return data;

    }

    //根据款号和品名搜索FAB卖点培训
    @Override
    public List<FABVo> findFabByCodeOrName(String token, String code) {
        if (null == token || token.isEmpty()){
            throw  new UploadException("没有穿ip");
        }
        String account = accountMapper.GetAccountForToken(token);
        List<FABVo> fabEntities = androidMapper.findFabByCodeOrName(account,code);
        for (FABVo fabEntity:fabEntities) {
            fabEntity.setBanner(androidMapper.getImgFAB(fabEntity.getId()));
        }
        return fabEntities;
    }

    /**
     * 根据条码查找商品款号
     * @param token
     * @param code
     * @return
     */
    @Override
    public String getProductByCode(String token, String code) {
        Info info = GetInfo(token);
        getProductByCode getProductByCode = new getProductByCode(info.getUrl(),info.getAccount(),info.getPassword());
        String productName = null;
        try {
            productName=getProductByCode.getData(code);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return productName;
    }


}
